package com.ecdc.soexample;

import java.io.File;
import java.io.FileOutputStream;
import java.lang.Thread.UncaughtExceptionHandler;
import java.sql.Date;
import java.text.SimpleDateFormat;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

public class CrashHandler implements UncaughtExceptionHandler {

	private static CrashHandler instance;
	// 单例引用，这里我们做成单例的，因为我们一个应用程序里面只需要一个UncaughtExceptionHandler实例
	private Context context;
//	private Handler handle = new Handler() {
//		public void handleMessage(android.os.Message msg) {
//			MainActivity2 mainActivity2 = new MainActivity2();
//			mainActivity2.setWifiStatus();
//			mainActivity2.requestData();
//			System.out.println("有异常");
//			Toast.makeText(MyApplication.context, "有异常", 1).show();
////			Toast.makeText(MyApplication.context, "测试代码弹框\njava.lang.IndexOutOfBoundsException", 1).show();
//		}
//	};

	private CrashHandler() {
	}

	public synchronized static CrashHandler getInstance() { // 同步方法，以免单例多线程环境下出现异常
		if (instance == null) {
			instance = new CrashHandler();
		}
		return instance;
	}

	public void init(Context ctx) { // 初始化，把当前对象设置成UncaughtExceptionHandler处理器
		Thread.setDefaultUncaughtExceptionHandler(this);
		this.context=ctx;

	}

	@Override
	public void uncaughtException(Thread thread, Throwable ex) { // 当有未处理的异常发生时，就会来到这里。。
//		SharedPreferences sp = App.context.getSharedPreferences("peizhi", Context.MODE_PRIVATE);
//		Editor editor = sp.edit();
//		editor.putString("isRecover", "重启");
//		editor.commit();
//		System.out.println("有异常!!!");
		getString(ex.toString());


//		/*
//		 * 软件重启
//		 */
//
//		Intent mStartActivity = new Intent(context, MainActivity2.class);
//		int mPendingIntentId = 123456;
//		PendingIntent mPendingIntent = PendingIntent.getActivity(context, mPendingIntentId,    mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
//		AlarmManager mgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
//		mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
//		System.exit(0);
		
		
		

	}

	public void getString(String str) {

		String filePath = null;

		boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日   HH:mm:ss");
		Date curDate = new Date(System.currentTimeMillis());

		if (hasSDCard) {

			filePath = Environment.getExternalStorageDirectory().toString() + "/日志" + "/Log" + formatter.format(curDate) + ".txt";

		} else

			filePath = Environment.getDownloadCacheDirectory().toString() + "/日志" + "/Log" + formatter.format(curDate) + ".txt";

		try {

			File file = new File(filePath);

			if (!file.exists()) {

				File dir = new File(file.getParent());

				dir.mkdirs();

				file.createNewFile();

			}

			FileOutputStream outStream = new FileOutputStream(file);

			outStream.write(str.getBytes());

			outStream.close();

		} catch (Exception e) {

			e.printStackTrace();

		}

	}

}
